package com.cafuc.discipline.respone;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.models.auth.In;
import lombok.Data;

/**
 * @Description: 返回结果封装
 * @Auther: Mete
 * @Date: 2022/11/14
 */

@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Result<T> {

    private Integer code;

    private String message;

    private T data;

    private Integer pageIndex;

    private Integer pageSize;

    private Integer total;


    /**
     * 成功
     */
    public static Result<Void> success() {
        Result<Void> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }


    /**
     * 成功，有返回数据
     */
    public static <V> Result<V> success(V data) {
        Result<V> result = new Result<>();
        result.code = ResultCode.SUCCESS.getCode();
        result.message = ResultCode.SUCCESS.getMessage();
        result.data = data;
        return result;
    }
    /**
     * 成功，有返回数据和信息
     */
    public static <V> Result<V> success(V data,String message) {
        Result<V> result = new Result<>();
        result.code = ResultCode.SUCCESS.getCode();
        result.message = ResultCode.SUCCESS.getMessage();
        result.data = data;
        result.message = message;
        return result;
    }

    /**
     * 成功,返回分页数据
     */
    public static <V> Result<V> success(V data, Integer pageIndex, Integer pageSize,Integer total) {
        Result<V> result = new Result<>();
        result.code = ResultCode.SUCCESS.getCode();
        result.message = ResultCode.SUCCESS.getMessage();
        result.data = data;
        result.pageIndex = pageIndex;
        result.pageSize = pageSize;
        result.total = total;
        return result;
    }

    /**
     * 失败
     */
    public static Result<Void> failure() {
        Result<Void> result = new Result<>();
        result.setCode(ResultCode.FAILURE.getCode());
        result.setMessage(ResultCode.FAILURE.getMessage());
        return result;
    }

    /**
     * 成功，有返回数据
     */
    public static <V> Result<V> failure(V data,String message) {
        Result<V> result = new Result<>();
        result.code = ResultCode.FAILURE.getCode();
        result.message = message;
        result.data = data;
        return result;
    }

    /**
     * 失败，自定义失败信息
     */
    public static Result<Void> failure(String message) {
        Result<Void> result = new Result<>();
        result.setCode(ResultCode.FAILURE.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 失败，使用已定义枚举
     */
    public static Result<Void> failure(ResultCode resultCode) {
        Result<Void> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    /**
     * 失败,参数错误
     */
    public static Result<Void> paramError() {
        Result<Void> result = new Result<>();
        result.setCode(ResultCode.REQUEST_PARAM_ERROR.getCode());
        result.setMessage(ResultCode.REQUEST_PARAM_ERROR.getMessage());
        return result;
    }

    public void setPage(Integer pageIndex, Integer pageSize, Integer total){
       this.pageIndex = pageIndex;
       this.pageSize = pageSize;
       this.total = total;

    }
}